<template>
  <el-dialog :visible="showDialog" @close="btnCancel">

    <!-- title插槽 -->
    <span slot="title">{{ title }}</span>

    <el-form ref="addUser" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="目录名称" prop="tagName">
        <el-input v-model="formData.tagName" style="width:50%" placeholder="请输入角色" />
      </el-form-item>
      <el-form-item label="所属学科" prop="subjectID">
        <el-select v-model="formData.subjectID" style="width:50%" placeholder="请选择权限组" @focus="getSubjectList(page)">
          <el-option v-for="item,index in subjectList" :key="index" :label="item.subjectName" :value="item.id" />
        </el-select>
      </el-form-item>
    </el-form>

    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button type="primary" size="small" @click="btnok">确定</el-button>
        </el-col>
      </el-row>
    </template>

  </el-dialog>
</template>

<script>
import { getSubjectList, addLabel, editLabel } from '@/api/label'

export default {
  props: {
    showDialog: { // 控制弹窗的显示隐藏
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      title: '新增目录',
      subjectList: [],
      // 表单数据
      formData: {
        tagName: '',
        subjectID: ''
      },
      // 表单数据验证规则
      rules: {
        tagName: [{ required: true, message: '不能为空', trigger: 'blur' }],
        subjectID: [{ required: true, message: '不能为空', trigger: 'blur' }]
      },
      inputShow: true
    }
  },
  methods: {
    // 取消、关闭弹窗
    btnCancel() {
      // 清空表单数据
      this.formData = {
        tagName: '',
        subjectID: ''
      }
      // 重置校验结果
      this.$refs.addUser.resetFields()
      // 关闭弹窗
      this.$emit('update:showDialog', false)
      this.title = '新增标签'
      this.inputShow = true
    },
    // 确定按钮
    async btnok() {
      if (this.formData.id) { // 编辑
        console.log(this.formData, '=============')
        // 1.调用方法
        // this.formData.create_time = null
        // this.formData.last_update_time = null
        // console.log(this.formData, '=-------------=')
        await editLabel(this.formData.id, this.formData)
        // 2.关闭弹窗
        this.$emit('update:showDialog', false)
        // 3.刷新页面
        this.$emit('reAfterAdd')
        // 4.提示信息
        this.$message.success('编辑标签信息成功！')
        //
      } else { // 添加
        // 1.调用方法
        console.log(this.formData, 'add add add')
        await addLabel(this.formData)
        // 2.关闭弹窗
        this.$emit('update:showDialog', false)
        // 3.刷新页面
        this.$emit('reAfterAdd')
        // 4.提示信息
        this.$message.success('添加标签成功！')
      }
    },
    // 添加用户方法
    async addLabel(data) {
      await addLabel(data)
    },
    // 获得权限组
    async getSubjectList() {
      const res = await getSubjectList()
      console.log(res, '======!!!getSubjectList!!!!======')
      this.subjectList = res.items
      // console.log(this.permissionsGroup)
    },
    // 编辑回填
    async rePut(row) {
      this.formData = row
      // this.formData.subjectID = row.subjectID
      // this.formData.subjectName = row.subjectName
      // this.formData.tagName = row.tagName
      this.title = '编辑标签'
      this.inputShow = false
      // console.log('这是子组件，拿到了当前点击的USERID，下面开始获取详细用户信息', id)
      // const res = await getSubjectList()
      // console.log(res, '这是获得的详细用户信息')
      // this.formData = res.items
      // console.log(this.formData, '111111111111111111')
    }
  }

}

</script>

<style>

</style>
